<template>
    <van-form @submit="onSubmit">
        <van-cell-group inset>
            <van-field
                v-model="form.account"
                name="account"
                label="用户名"
                placeholder="用户名"
                :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
                v-model="form.password"
                type="password"
                name="password"
                label="密码"
                placeholder="密码"
                :rules="[{ validator:validator1 }]"
            />
            <van-field
                v-model="form.secpassword"
                type="password"
                name="secpassword"
                label="密码确认"
                placeholder="密码确认"
                :rules="[{ validator }]"
            />
        </van-cell-group>
        <div style="margin: 16px">
            <van-button round block type="primary" native-type="submit"> 提交 </van-button>
        </div>
    </van-form>
</template>
<script lang="ts" setup>
import { reactive } from "vue"
import {register,registerInput} from "../api"
import { showNotify } from "vant";
const form = reactive<registerInput>({
    account: "",
    password: "",
    secpassword: ""
})
const validator = (val: any) => {
    if (val.trim() === "") {
        return `${val} 不合法，请重新输入`
    }
    if (val != form.password) {
        return "两次输入的密码不一致"
    }
    return true
}
const validator1 = (val: any) => {
    if (val.trim() === "") {
        return `${val} 不合法，请重新输入`
    }
    return true
}
const onSubmit = async(values: any) => {
    console.log("submit", values)
    delete values.secpassword
    let res=await register(values)
    console.log(res)
    if (res.code === 0) {
        showNotify({ type: "primary", message: "注册成功!" })
    } else {
        showNotify({ type: "danger", message: res.msg! })
    }
}
</script>
<style lang="less" scoped></style>
